package cn.htcode.service.imp;

import cn.htcode.domain.Product;
import cn.htcode.mapper.ProductMapper;
import cn.htcode.service.IProductService;
import cn.htcode.util.MybatisUtil;
import org.apache.ibatis.session.SqlSession;

import java.io.IOException;
import java.util.List;

/**
 * @author: Mr_he
 * @Version:1.0
 */
public class ProductImpl implements IProductService {
    @Override
    public List<Product> list(String currentPage, String pageSize, String name) {
        try (SqlSession session = MybatisUtil.getSqlSession()) {
            ProductMapper mapper = session.getMapper(ProductMapper.class);
//            PageHelper.startPage(Integer.valueOf(currentPage), Integer.valueOf(pageSize));
            List<Product> list = mapper.list(name);
            return list;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public int insert(Product product) {
        try (SqlSession session = MybatisUtil.getSqlSession()) {
            ProductMapper mapper = session.getMapper(ProductMapper.class);
            return mapper.insert(product);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public int update(Product product) {
        try (SqlSession sqlSession = MybatisUtil.getSqlSession()) {
            final ProductMapper mapper = sqlSession.getMapper(ProductMapper.class);
            return mapper.updateByPrimaryKey(product);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public int delete(Long id) {
        try (SqlSession sqlSession = MybatisUtil.getSqlSession()) {
            final ProductMapper mapper = sqlSession.getMapper(ProductMapper.class);
            return mapper.deleteByPrimaryKey(id);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }
}